package {{.pkg}}

import (
	"context"
	"encoding/json"
	"fmt"
	
	"orchestrator/internal/infra/database/mysql/po"

	"github.com/osamingo/jsonrpc/v2"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
	// "github.com/zeromicro/go-zero/core/logc"
)

type {{.upperStartCamelObject}}InsertParams struct {
	*{{.upperStartCamelObject}}
	*TxID
}

type {{.upperStartCamelObject}}InsertResult struct {
}

type {{.upperStartCamelObject}}InsertFunc struct {
	Db sqlx.SqlConn
	Repo po.{{.upperStartCamelObject}}Model
	{{.upperStartCamelObject}}InsertParams
	{{.upperStartCamelObject}}InsertResult
}

func (t *{{.upperStartCamelObject}}InsertFunc) Name() string {
	return fmt.Sprintf("%s.%s", "{{.table}}", "Insert")
}

func (t *{{.upperStartCamelObject}}InsertFunc) ServeJSONRPC(c context.Context, params *json.RawMessage) (result any, err *jsonrpc.Error) {
	fmt.Printf("ServeJSONRPC %s( %s )\n ", t.Name(), *params)

	p := []{{.upperStartCamelObject}}InsertParams{}
	ppp := {{.upperStartCamelObject}}InsertParams{ {{.upperStartCamelObject}}: &{{.upperStartCamelObject}}{} }
	tx := {{.upperStartCamelObject}}InsertParams{TxID: &TxID{}}
	p = append(p, ppp)
	p = append(p, tx)

	err1 := json.Unmarshal(*params, &p)
	if err1 != nil {
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	// 使用 json.MarshalIndent() 函数格式化输出
	pj, err1 := json.MarshalIndent(p, "", "  ")
	if err1 != nil {
		fmt.Println("Error:", err1)
		return nil, &jsonrpc.Error{
			Code:    jsonrpc.ErrorCodeInvalidRequest,
			Message: err1.Error(),
		}
	}

	fmt.Printf("%s:  %s\n", t.Name(), pj)
	return t.Result(), nil
}

func (t *{{.upperStartCamelObject}}InsertFunc) Params() any {
	return t.{{.upperStartCamelObject}}InsertParams
}

func (t *{{.upperStartCamelObject}}InsertFunc) Result() any {
	return t.{{.upperStartCamelObject}}InsertResult
}
